package com.fowo.api.common.controller;

import com.fowo.api.common.model.R;
import com.fowo.api.common.model.RException;
import org.apache.ibatis.builder.BuilderException;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.validation.BindException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;

/**
 * 错误返回处理
 */
@RestControllerAdvice
@ResponseBody
public class ErrorHandleController {
    @ExceptionHandler(BindException.class)
    public R validationErrorHandler(BindException exception) {
        return R.fail("数据校验失败", exception.getAllErrors());
    }

    @ExceptionHandler(RException.class)
    public R resultErrorHandle(RException exception) {
        return exception.getResult();
    }


    @ExceptionHandler(HttpMessageNotReadableException.class)
    public R HttpMessageNotReadableException(HttpMessageNotReadableException exception) {
        return R.fail(exception.getLocalizedMessage(),exception.getLocalizedMessage());
    }

    @ExceptionHandler(MethodArgumentTypeMismatchException.class)
    public R HttpMessageNotReadableException(MethodArgumentTypeMismatchException exception) {
        return R.fail(exception.getLocalizedMessage(),exception.getLocalizedMessage());
    }

    @ExceptionHandler(BuilderException.class)
    public R BuilderException(BuilderException exception) {
        return R.fail(exception.getLocalizedMessage(),exception.getLocalizedMessage());
    }
    @ExceptionHandler(IllegalArgumentException.class)
    public R IllegalArgumentException(IllegalArgumentException exception) {
        return R.fail(exception.getLocalizedMessage(),exception.getLocalizedMessage());
    }
}
